package com.vsoft.sms_cm;
//用于数据处理,补录客户群组和模板群组。
import java.io.DataOutputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.log4j.Logger;

import com.vsoft.framework.core.BaseException;
import com.vsoft.framework.core.BusiObj;
import com.vsoft.framework.core.Configuration;
import com.vsoft.framework.core.db.DBGadgets;
import com.vsoft.framework.core.db.DBTrans;
import com.vsoft.framework.core.db.OperAgent;
import com.vsoft.framework.core.db.SQLArgs;
import com.vsoft.framework.core.esb.ServiceBus;
import com.vsoft.framework.core.util.BOHelper;
import com.vsoft.framework.core.util.LogCollect;
import com.vsoft.framework.core.util.XMLUtil;
import com.vsoft.framework.server.servlet.SessionData;
import com.vsoft.framework.server.servlet.HttpUtil;
import com.vsoft.anti_fra.*;

public class TestInputGroup {

	private static Logger vLogger = Logger.getLogger(TestQuery.class);
	private static String vName = TestInputGroup.class.getName();
	private SessionData vSD = null;
	private String vDebugHome = "";
	private BusiObj				vInBO		=  new BusiObj();
	private SQLArgs			vArg				= new SQLArgs();
	
	public static void main(String[] args)  {
	
		try{

			vLogger.info("---------------InitSystem-------------");
			Configuration.InitSystem();
			vLogger.info("---------------generate test data-------------");

			TestInputGroup t = new TestInputGroup();
			Connection conn = DBTrans.getConnection();
			String listName=	"RESULT";
			
			String sql	=	"select * from sms_templete where state=1 and ent_code in(" +
								" select distinct ent_code from sms_templete where state=1 " +
								" minus " +
								" select distinct ent_code from customer_group where state=1" +
								" )";
/*			String order_num=args[0];
			String area_no=args[1];*/
			t.vArg.reset();
			//
/*			t.vArg.setArg("order_num", order_num);
			t.vArg.setArg("area_no", area_no);*/
			BusiObj rsBo 						= OperAgent.execQuery(conn, "Query log_info " ,sql, t.vArg);
		    vLogger.info("查询log，返回数据是:\n" + BOHelper.boToXML(rsBo));
		    ArrayList<BusiObj> Al_ent = rsBo.getBusiObjList(listName);
		    int size=Al_ent.size();
		    if(size==0)//
		    {
		    	conn.close();
		    	vLogger.info("没有数据。。");
		    	//throw new BaseException("错误","根据号码"+order_num+"在日志中找不到信息");
		    }
		    int count=0;
		    for(BusiObj m:Al_ent)
		    {
		    	count++;
		    	String ent_code = m.getString("ent_code");
		    	String sms_temple_id = m.getString("sms_temple_id");
		    	String group_id ="";
				String	sql_group_id=	"select max(group_id)+1 from customer_group ";
				t.vArg.reset();				
				group_id	=	OperAgent.QueryString(conn, "Query seqName", sql_group_id, t.vArg);
				vLogger.info("=====================获取最大group_id   sql:" + sql_group_id+"" +
						"\n group_id value="+group_id);
				
		    	String sql_customer_group =" insert into customer_group" +
		    			" ( GROUP_ID,GROUP_NAME,STATE,NOTES,OPER_TIME,ENT_CODE,GROUP_TYPE) " +
		    			" values(:group_id,'默认群组','1','所有拨打企业电话的手机用户',sysdate,:ent_code,'0') ";
		    	t.vArg.reset();
		    	t.vArg.setArg("group_id", group_id);
		    	t.vArg.setArg("ent_code", ent_code);
		    	OperAgent.execUpdate(conn, " insert into customer_group ",sql_customer_group, t.vArg);
		    	vLogger.info("===================== insert into customer_group   sql:" + sql_customer_group);
		    	
		    	String temple_group_sn ="";
				String	sql_temple_group_sn=	"select max(temple_group_sn)+1 from sms_temple_group ";
				t.vArg.reset();				
				temple_group_sn	=	OperAgent.QueryString(conn, "Query seqName", sql_temple_group_sn, t.vArg);
				vLogger.info("=====================获取最大temple_group_sn   sql:" + sql_temple_group_sn+"" +
						"\n temple_group_sn value="+temple_group_sn);
				
		    	String sql_sms_temple_group =" insert into sms_temple_group" +
    			" ( TEMPLE_GROUP_SN,GROUP_ID,SMS_TEMPLE_ID,GROUP_TYPE) " +
    			" values(:temple_group_sn,:group_id,:sms_temple_id,'0') ";
		    	t.vArg.reset();
		    	t.vArg.setArg("group_id", group_id);
		    	t.vArg.setArg("temple_group_sn", temple_group_sn);
		    	t.vArg.setArg("sms_temple_id", sms_temple_id);
		    	OperAgent.execUpdate(conn, " insert into sms_temple_group ",sql_sms_temple_group, t.vArg);
				
				vLogger.info("===================== insert into customer_group   sql:" + sql_sms_temple_group);
		    }


			vLogger.info("共补录记录:\n  :" +count);
//			t.vInBO.delete("UPDATE_LOG_TIME");

			
			DBGadgets.check(conn);
			conn.rollback();
			//conn.commit();
			conn.close();
			//////////////////////////////

		}
		catch(SQLException e)
		{
			e.printStackTrace();
		}
		catch(BaseException e)
		{
			e.printStackTrace();
			vLogger.error(e.ErrMethod);
			vLogger.error(e.SysMsg);
		}
			vLogger.info("---------------Game Over-------------");
	}	
	
		

	private int  init()
	{
		int iRet = 0;
		vSD = new  SessionData();
		vSD.vRoleCode = new String[] {"1"};
		vSD.vStaffCode = "1";
		vSD.vStaffNo = "0001";
		vSD.vStaffName = "test";
		
	
		vDebugHome = System.getProperty("DEBUG_HOME");
		if (vDebugHome == null)
		{
			iRet = -1;
			vLogger.error("需要设置DEBUG_HOME环境变量来指定debug 文件所在的目录");
		}
		return iRet;
		
	}
	
}
